<template>
  <div class="main-container">
    <div class="register-container">
      <el-form
          ref="registerForm"
          :model="registerForm"
          :rules="registerRules"
          class="register-form"
      >
        <h3 class="title">智慧监护系统</h3>
        <el-button index="/views/ManagerLoginView" type="text" @click="$router.push('/')">返回登录</el-button>
        <el-form-item prop="username">
          <el-input
              v-model="registerForm.username"
              type="text"
              placeholder="用户名"
              prefix-icon="el-icon-user-solid"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="registerForm.password"
              type="password"
              prefix-icon="el-icon-s-help"
              placeholder="密码"
              show-password
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
              v-model="registerForm.repassword"
              type="repassword"
              prefix-icon="el-icon-s-help"
              placeholder="请再次输入密码"
              show-password
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input
              v-model="registerForm.email"
              type="email"
              prefix-icon="el-icon-chat-line-square"
              placeholder="邮箱"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="verify">
          <el-input  v-model="registerForm.verify" placeholder="请输入验证码">
            <template slot="append">
              <el-button @click.native.prevent="email" type="primary">发送验证码</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item style="width: 100%;display: flex;justify-content: center">
          <el-button
              :loading="loading"
              size="medium"
              @click.native.prevent="handleRegister"
              v-loading.fullscreen.lock="loading"
          >
            <span v-if="!loading">注册</span>
            <span v-else>注 册 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="spaceMan-parent">
    </div>
  </div>
</template>

<script>
import {messageInfo} from "@/utils/MessageInfo";
import {Email, Register} from "../../api/loginApi";

export default {
  name: "RegisterView",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === ''||value !== this.registerForm.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      registerForm: {
        username: "",
        password: "",
        email:"",
        verify:"",
      },
      registerRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的用户名"},
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"},
          ],
        repassword: [
          {required: true, validator: validatePass},
        ],
        email: [
          {required: true, trigger: "blur", message: "请输入邮箱"},
        ],
      },
    };
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (!valid || !this.registerForm.verify){
          messageInfo({type: 'error',message: "请填邮箱验证码"});
          return;
        }
        this.loading = true;
        Register({
          username:this.registerForm.username,
          password:this.registerForm.password,
          email:this.registerForm.email,
          verify:this.registerForm.verify
        }).then((res) => {
          if (res) {//请求成功的相应值
            console.log(res)
            this.loading = false;
            this.$router.replace("/");
            messageInfo({message: "注册成功！"});
          } else {
            this.loading = false;
          }
        })
      });
    },
    email(){
      this.$refs.registerForm.validate((valid) => {
        if (!valid) return;
        this.loading = true;
        Email({
          username:this.registerForm.username,
          email:this.registerForm.email
        }).then((res) => {
          console.log(res)
          if (res) {//请求成功的相应值
            console.log(res)
            messageInfo({message: "邮箱验证码已发送"});
          }
        })
      })
      this.loading=false
    }

  },
};
</script>

<style scoped>
.main-container {
  position: relative;
  width: 100%;
  height: 100%;
}

.register-container {
  z-index: 1;
  position: absolute;
  left: 0;
  height: 100%;
  width: 30%;
  background-color: #E4F2F5;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 6px;
  box-shadow: 2px 3px 3px #888888;
  opacity: 0.9;
}

.register-form {
  z-index: 1;
  width: 85%;
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
  font-size: 32px;
}

.spaceMan-parent {
  position: absolute;
  right: 10%;
  width: 50%;
  min-height: 100%;
  background-image: url("../../assets/login/back1.jpg");
  background-size: cover;
  background-position: center;
  overflow: hidden;
}

</style>